package com.jereh.wymusicapplication.activity.fragment;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.jereh.wymusicapplication.R;
import com.jereh.wymusicapplication.activity.Application.PlayApplication;
import com.jereh.wymusicapplication.activity.Localmusic.FindSongs;
import com.jereh.wymusicapplication.activity.adapter.DanQuAdapter;
import com.jereh.wymusicapplication.activity.entity.Mp3Info;
import com.jereh.wymusicapplication.activity.sql.DBDao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by ASUS on 2016/12/15.
 */
public class DanQuFragment extends Fragment {
    private ContentResolver resolver;
    private ListView listView;
    private FindSongs findSongs;
    private DanQuAdapter adapter;
    private List<Mp3Info> list =new ArrayList<>();
    private PlayApplication app;
    private Intent intent;
    private int no;
    private View view;
    private SwipeRefreshLayout refresh;
    private Timer timer;
    private DBDao dbDao;
    private List<Mp3Info> musiclist =new ArrayList<>();
    private int icon=0;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         view =LayoutInflater.from(getActivity()).inflate(R.layout.dan_qu_fragment, null);
        listView= (ListView) view.findViewById(R.id.listView);
        findSongs =new FindSongs();
        resolver =getActivity().getContentResolver();
        list.addAll(findSongs.getMp3Infos(resolver));
        adapter=new DanQuAdapter(getActivity(),list);
        listView.setAdapter(adapter);
        app= (PlayApplication) getActivity().getApplication();
        TimerUpdateView();

        initClickListView();

        initrefresh();

        dbDao=new DBDao(getActivity());
        musiclist.addAll(list);

            dbDao.allMusicContent(musiclist);

        return view;
    }




    public void initClickListView(){

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                initImg();
                no = position;
                icon=1;
                app.title = list.get(position).getTitle();
                app.artist = list.get(position).getArtist();
                Intent intent = new Intent();
                intent.setAction("com.jereh.music.initmusic");
                intent.putExtra("songs", (Serializable) list);
                intent.putExtra("no", position);
                getActivity().sendBroadcast(intent);

            }
        });

    }

    private void initImg() {
        //设置小喇叭是否显示
        if (icon == 1) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setIsvisible(false);
            }

            list.get(app.songPosition).setIsvisible(true);
            adapter.notifyDataSetChanged();
        }
    }
    //开启子线程刷新界面
    public  Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==1){
                initImg();
            }
        }
    };
    private void TimerUpdateView(){
         timer =new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                 Message msg =handler.obtainMessage();
                msg.what=1;
                handler.sendMessage(msg);
            }
        },0,500);
    }

    private void initrefresh(){
        //刷新
        refresh= (SwipeRefreshLayout) view.findViewById(R.id.refresh);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "关闭动画了啊！！", Toast.LENGTH_SHORT).show();
                        refresh.setRefreshing(false);
                    }
                },1000);//3秒关闭动画
            }
        });
        /**
         * 设置箭头颜色，走一圈换一个颜色
         */
        refresh.setColorSchemeColors(Color.RED,Color.RED,Color.RED);
        //整个的背景颜色
        //refresh.setBackgroundColor(Color.GRAY);
        //圆圈的颜色
       // refresh.setProgressBackgroundColorSchemeColor(Color.GRAY);
    }


}
